package com.hong.concurrent.chapter3;

/**
 * java内存模型CPU缓存数据不一致问题
 *
 */
public class VolatileTest2 {

    //值
    private volatile   static int INIT_VALUE=0;
    //最大值
    private final static int MAX_LIMIT=50;

    public static void main(String[] args) {
        new Thread(()->{
            while (INIT_VALUE<MAX_LIMIT){
                System.out.println("T1->"+(++INIT_VALUE));
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"ADDER-1").start();

        new Thread(()->{
            while (INIT_VALUE<MAX_LIMIT){
                System.out.println("T2->"+(++INIT_VALUE));
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"ADDER-2").start();

    }
}
